package com.lagou.homework.util;

import java.sql.*;

public class DbUtil {
    private static String jdbcDriverName;
    private static String dbUrl;
    private static String dbUserName;
    private static String dbPassword;

    static {
        jdbcDriverName = "com.mysql.jdbc.Driver";
        dbUrl = "jdbc:mysql://localhost:3306/db_web?characterEncoding=UTF-8";
        dbUserName = "root";
        dbPassword = "235811";
        try {
            Class.forName(jdbcDriverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接
     * @return
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
    }

    /**
     * 关闭资源
     * @param con
     * @throws SQLException
     */
    public static void close(Connection con) throws SQLException {
        if (null != con) {
            con.close();
        }
    }

    public static void close(Connection con, Statement st) throws SQLException {
        if (null != st) {
            st.close();
        }
        close(con);
    }

    public static void close(Connection con, Statement st, ResultSet rs) throws SQLException {
        if (null != rs) {
            rs.close();
        }
        close(con, st);
    }
}
